package cn.dengta.webapp.user.model;

import java.util.Date;
import javax.annotation.Nullable;

import cn.dengta.common.model.ValueProxy;
import cn.dengta.context.model.LabelI18nProxy;
import cn.dengta.context.model.UserEnumI18nKey;
import lombok.Getter;
import me.codeplayer.util.EasyDate;

@Getter
public enum Zodiac implements LabelI18nProxy<Zodiac, Integer> {

	/** 摩羯座12.22-1.19 */
	CAPRICORN(1, UserEnumI18nKey.ZODIAC_CAPRICORN, 20),
	/** 水瓶座1.20-2.18 */
	AQUARIUS(2, UserEnumI18nKey.ZODIAC_AQUARIUS, 19),
	/** 双鱼座2.19-3.20 */
	PISCES(3, UserEnumI18nKey.ZODIAC_PISCES, 21),
	/** 白羊座3.21-4.19 */
	ARIES(4, UserEnumI18nKey.ZODIAC_ARIES, 20),
	/** 金牛座4.20-5.20 */
	TAURUS(5, UserEnumI18nKey.ZODIAC_TAURUS, 21),
	/** 双子座5.21-6.21 */
	GEMINI(6, UserEnumI18nKey.ZODIAC_GEMINI, 22),
	/** 巨蟹座6.22-7.22 */
	CANCER(7, UserEnumI18nKey.ZODIAC_CANCER, 23),
	/** 狮子座7.23-8.22 */
	LEO(8, UserEnumI18nKey.ZODIAC_LEO, 23),
	/** 处女座8.23-9.22 */
	VIRGO(9, UserEnumI18nKey.ZODIAC_VIRGO, 23),
	/** 天秤座9.23-10.23 */
	LIBRA(10, UserEnumI18nKey.ZODIAC_LIBRA, 24),
	/** 天蝎座10.24-11.22 */
	SCORPIO(11, UserEnumI18nKey.ZODIAC_SCORPIO, 23),
	/** 射手座11.23-12.21 */
	SAGITTARIUS(12, UserEnumI18nKey.ZODIAC_SAGITTARIUS, 22),
	//
	;

	// 定义私有变量
	public final Integer value;
	public final Integer maxLimit;
	final ValueProxy<Zodiac, Integer> proxy;

	// 构造函数，枚举类型只能为私有
	Zodiac(Integer value, String label, Integer maxLimit) {
		this.value = value;
		this.maxLimit = maxLimit;
		this.proxy = new ValueProxy<>(this, value, label);
	}

	public static Zodiac[] getValues() {
		return ValueProxy.getCachedArray(Zodiac.class, Zodiac::values);
	}

	public static Zodiac of(Integer value) {
		return CAPRICORN.getValueOf(value);
	}

	public static String calcConstellation(@Nullable Date birthday) {
		if (birthday == null) {
			return null;
		}
		EasyDate d = new EasyDate(birthday);
		int month = d.getMonth();
		Zodiac zodiac = Zodiac.of(month);
		if (d.getDay() >= zodiac.maxLimit) {
			zodiac = Zodiac.of(month >= 12 ? 1 : month + 1);
		}
		return zodiac.getLabel();
	}

	public final static String[] ZODIAC = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

	/**
	 * 通过生日计算属相
	 */
	public static String calcZodiac(Date birthday) {
		if (birthday == null) {
			return null;
		}
		int year = new EasyDate(birthday).getYear();
		final int start = 1900;
		return year < start ? null : ZODIAC[(year - start) % ZODIAC.length];
	}

}
